package lingPipeImplementacion;


import com.aliasi.cluster.Clusterer;
import com.aliasi.cluster.CompleteLinkClusterer;
import com.aliasi.cluster.HierarchicalClusterer;
import com.aliasi.cluster.Dendrogram;
import com.aliasi.cluster.SingleLinkClusterer;

import com.aliasi.spell.EditDistance;
import com.aliasi.spell.FixedWeightEditDistance;

import com.aliasi.util.Distance;

import extractorTwitter.Timeline;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;

import twitter4j.Tweet;

public class ClusterModel 
{

	static final Distance<CharSequence> EDIT_DISTANCE = new EditDistance(false);

	public static void main(String[] args) 
	{
		Timeline tl= new Timeline();
		String palabrasClave[] = {"Arturo Calle", "@Arturo_Calle"};
		Set<String> input=new HashSet<String>(Arrays.asList(palabrasClave));
		Collection<Tweet> busqueda;
		FileWriter fichero = null;
		PrintWriter pw = null;
		try
		{
			fichero = new FileWriter( "./data/resultados.txt" );
			pw = new PrintWriter( fichero );

			busqueda = tl.searchTweetsWithKeywords(input, 1);
			// parse out input set
			Set<String> inputSet = new HashSet<String>();
			for (Tweet t : busqueda) 
				inputSet.add(t.getText());

			// set up max distance
			int maxDistance = args.length == 1
			? Integer.MAX_VALUE
					: Integer.valueOf("1");

			// dump off-diagonal upper triangular distance matrix
			for (String s1 : inputSet) 
			{	
				for (String s2: inputSet)
				{
					if (s1.compareTo(s2) < 0)
					{
						System.out.println("distance(" + s1 + "," + s2 + ")=" + EDIT_DISTANCE.distance(s1,s2));
						pw.println("distance(" + s1 + "," + s2 + ")=" + EDIT_DISTANCE.distance(s1,s2));
					}
				}
			}


			// Single-Link Clusterer
			HierarchicalClusterer<String> slClusterer = new SingleLinkClusterer<String>(maxDistance, EDIT_DISTANCE);

			// Complete-Link Clusterer
			HierarchicalClusterer<String> clClusterer = new CompleteLinkClusterer<String>(maxDistance, EDIT_DISTANCE);

			// Hierarchical Clustering
			Dendrogram<String> slDendrogram = slClusterer.hierarchicalCluster(inputSet);
			pw.println();
			pw.println();
			System.out.println("\nSingle Link Dendrogram");
			pw.println("\nSingle Link Dendrogram");
			System.out.println(slDendrogram.prettyPrint());
			pw.println(slDendrogram.prettyPrint());
			pw.println();
			pw.println();
			pw.println("\nSingle Link Dendrogram - toString");
			pw.println(slDendrogram.toString());

			Dendrogram<String> clDendrogram = clClusterer.hierarchicalCluster(inputSet);
			pw.println();
			pw.println();
			System.out.println("\nComplete Link Dendrogram");
			pw.println("\nComplete Link Dendrogram");
			System.out.println(clDendrogram.prettyPrint());
			pw.println(clDendrogram.prettyPrint());

			// Dendrograms to Clusterings
			pw.println();
			pw.println();
			System.out.println("\nComplete Link Clusterings");
			pw.println("\nComplete Link Clusterings");
			
			for (int k = 1; k <= clDendrogram.size(); ++k) {
				Set<Set<String>> clKClustering = clDendrogram.partitionK(k);
				System.out.println(k + "  " + clKClustering);
				pw.println(k + "  " + clKClustering);
			}

			pw.println();
			pw.println();
			System.out.println("\nSingle Link Clusterings");
			pw.println("\nSingle Link Clusterings");
			for (int k = 1; k <= slDendrogram.size(); ++k) 
			{
				Set<Set<String>> slKClustering = slDendrogram.partitionK(k);
				System.out.println(k + "  " + slKClustering);
				pw.println(k + "  " + slKClustering);
			}

			Set<Set<String>> clClustering = clClusterer.cluster(inputSet);
			pw.println();
			pw.println();
			System.out.println("\n\nComplete Link Clustering");
			pw.println("\n\nComplete Link Clustering");
			System.out.println(clClustering);
			pw.println(clClustering);

			Set<Set<String>> slClustering = slClusterer.cluster(inputSet);
			pw.println();
			pw.println();
			System.out.println("\nSingle Link Clustering");
			pw.println("\nSingle Link Clustering");
			System.out.println(slClustering);
			pw.println(slClustering);
			
			pw.close();
			fichero.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
